<template>
    <div>

        <p>
            <button class="btn btn-white btn-default btn-round" @click="refresh()">
                <i class="ace-icon fa fa-refresh red"></i>
                刷新
            </button>
            &nbsp;
            <button class="btn btn-white btn-success btn-round" @click="openAddModal()">
                <i class="ace-icon fa fa-edit blue"></i>
                新增
            </button>
        </p>

        <div class="row">
            <div v-for="teacher in teachers" class="col-md-3">
                <div>
				  <span class="profile-picture">
					<img style="width: 360px;height: 202px" v-show="!teacher.image" class="editable img-responsive editable-click editable-empty"
                         src="/static/image/讲师头像/头像1.jpg" v-bind:title="teacher.intro"/>
					<img style="width: 360px;height: 202px" v-show="teacher.image" class="editable img-responsive editable-click editable-empty"
                         v-bind:src="teacher.image" v-bind:title="teacher.intro"/>
				  </span>

                    <div class="space-4"></div>

                    <div class="width-85 label label-info label-xlg arrowed-in arrowed-in-right">
                        <div class="inline position-relative">
                            <a href="javascript:;" class="user-title-label dropdown-toggle" data-toggle="dropdown">
                                <i class="ace-icon fa fa-circle light-green"></i>
                                &nbsp;
                                <span class="white">{{teacher.position}}</span>
                            </a>
                        </div>
                    </div>
                </div>

                <div class="space-6"></div>

                <div class="text-center">
                    <a href="javascript:;" class="text-info bigger-110" v-bind:title="teacher.motto">
                        <i class="ace-icon fa fa-user"></i>
                        {{teacher.name}}【{{teacher.nickname}}】
                    </a>
                </div>

                <div class="space-6"></div>

                <div class="profile-social-links align-center">
                    <button v-on:click="openEditModal(teacher.id)" class="btn btn-white btn-xs btn-primary btn-round">
                        <i class="ace-icon fa fa-pencil bigger-120"></i>
                        编辑
                    </button>
                    &nbsp;
                    <button v-on:click="deleteById(teacher.id)"
                            class="btn btn-white btn-xs btn-danger btn-round">
                        <i class="ace-icon fa fa-trash-o bigger-120"></i>
                        删除
                    </button>
                </div>

                <div class="hr hr16 dotted"></div>

            </div>
        </div>

        <!-- 分页组件 -->
        <pagination ref="pagination" v-bind:list="list"></pagination>

        <!-- 模态框 -->
        <div id="myModal1" class="modal fade" tabindex="-1" role="dialog">
            <div class="modal-dialog" role="document">
                <div class="modal-content" style="border-color: #aa160e;background-color: lightblue">
                    <div class="modal-header">
                        <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span
                                aria-hidden="true">&times;</span></button>
                        <h4 class="modal-title bolder center">{{modalTitle}}</h4>
                    </div>
                    <div class="modal-body">
                        <form class="form-horizontal">
                            <div class="form-group">
                                <label class="col-sm-2 control-label">姓名</label>
                                <div class="col-sm-10">
                                    <input v-model="teacher.name" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">昵称</label>
                                <div class="col-sm-10">
                                    <input v-model="teacher.nickname" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">头像</label>
                                <div class="col-sm-10">
									<!--上传文件组件-->
									<BigFile v-bind:inputId="'image-upload'"
                                          v-bind:text="'上传图片'"
                                          v-bind:afterUpload="afterUpload"
                                          v-bind:suffixs="['jpg', 'jpeg', 'png']"
                                          v-bind:use="FILE_USE.TEACHER.key">

                                    </BigFile>
                                    <div v-show="teacher.image" class="row">
										<div class="col-md-4">
											<img v-bind:src="teacher.image" class="img-responsive">
										</div>
									</div>
                                </div>


                                <!--<div class="col-sm-10">
                                    <big-file v-bind:input-id="'image-upload'"
                                              v-bind:text="'上传头像'"
                                              v-bind:suffixs="['jpg', 'jpeg', 'png']"
                                              v-bind:use="FILE_USE.TEACHER.key"
                                              v-bind:after-upload="afterUpload"></big-file>
                                    <div v-show="teacher.image" class="row">
                                        <div class="col-md-4">
                                            <img v-bind:src="teacher.image" class="img-responsive">
                                        </div>
                                    </div>
                                </div>-->
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">职位</label>
                                <div class="col-sm-10">
                                    <input v-model="teacher.position" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">座右铭</label>
                                <div class="col-sm-10">
                                    <input v-model="teacher.motto" class="form-control">
                                </div>
                            </div>
                            <div class="form-group">
                                <label class="col-sm-2 control-label">简介</label>
                                <div class="col-sm-10">
                                    <textarea v-model="teacher.intro" class="form-control" rows="5"></textarea>
                                </div>
                            </div>
                        </form>
                    </div>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-default" data-dismiss="modal">取消</button>
                        <button v-on:click="save()" type="button" class="btn btn-primary">保存</button>
                    </div>
                </div><!-- /.modal-content -->
            </div><!-- /.modal-dialog -->
        </div><!-- /.modal -->
    </div>

</template>

<script>
    import Pagination from "../../components/pagination.vue"
	import BigFile from "../../components/big-file"

    export default {
        name: "teacher",
        components: {
            Pagination,BigFile
        },
        data: function () {
            return {
                teachers: [],
                teacher: {},
                modalTitleArr: {
                    insert: "新增讲师",
                    update: "修改讲师"
                },
                modalTitle: "",
                FILE_USE:FILE_USE
            }
        },
        mounted: function () {
            // this.$parent.activeSidebar("business-teacher-sidebar")
            let _this = this;
            _this.list();
        },
        methods: {
            /*上传文件之后*/
            afterUpload(data) {
                let _this = this;
                _this.teacher.image = data.data.path;
            },
            /* 打开新增模态框 */
            openAddModal() {
                let _this = this;
                _this.clearTeacher();
                _this.modalTitle = _this.modalTitleArr.insert;
                $("#myModal1").modal({backdrop: 'static'});
                $("#myModal1").modal("show");

            },
            /*打开编辑模态框*/
            openEditModal(id) {
                let _this = this;
                _this.clearTeacher();
                _this.modalTitle = _this.modalTitleArr.update;
                _this.$ajax.get(process.env.VUE_APP_SERVER + '/business/admin/teacher/findById/' + id).then((response) => {
                    _this.teacher = response.data.data;
                });
                $("#myModal1").modal({backdrop: 'static'});
                $("#myModal1").modal("show");
            },
            refresh() {
                let _this = this;
                Loading.show();
                _this.list(_this.$refs.pagination.page);
                Loading.hide();
            },
            /* 查找讲师 */
            list(page = 1) {
                let _this = this;
                _this.$ajax.post(process.env.VUE_APP_SERVER + '/business/admin/teacher/list', {
                    page: page,
                    size: _this.$refs.pagination.size
                }).then((response) => {
                    _this.teachers = response.data.data.list;
                    _this.$refs.pagination.render(page, response.data.data.total);
                })
            },
            /*保存讲师信息到后台*/
            save() {
                let _this = this;
                if (!_this.teacher.id) {
                    /*不存在id为新增*/
                    _this.insert();
                } else {
                    /*存在id为修改*/
                    _this.update();
                }
            },
            /*新增讲师*/
            insert() {
                let _this = this;
                _this.$ajax.post(process.env.VUE_APP_SERVER + '/business/admin/teacher/save', _this.teacher).then((response) => {
                    console.log(response);
                    if (response.data.success) {
                        _this.list(_this.$refs.pagination.page);
                        Toast.success(response.data.message);
                    } else {
                        Toast.error(response.data.message);
                    }
                    $("#myModal1").modal("hide");
                });
            },
            /*修改讲师*/
            update() {
                let _this = this;
                _this.$ajax.put(process.env.VUE_APP_SERVER + '/business/admin/teacher/update', _this.teacher).then((response) => {
                    if (response.data.success) {
                        _this.list(_this.$refs.pagination.page);
                        Toast.success(response.data.message);
                    } else {
                        Toast.error(response.data.message);
                    }
                    $("#myModal1").modal("hide");
                })
            },
            /*删除讲师*/
            deleteById(id) {
                let _this = this;
                /*删除提示框*/
                Confirm.show("", () => {
                    _this.$ajax.delete(process.env.VUE_APP_SERVER + '/business/admin/teacher/deleteById/' + id).then((response) => {
                        if (response.data.success) {
                            _this.list(_this.$refs.pagination.page);
                            Toast.success(response.data.message);
                        } else {
                            Toast.error(response.data.message);
                        }
                        $("#myModal1").modal("hide");
                    })
                });
            },
            /*清空模态框的数据*/
            clearTeacher() {
                let _this = this;
                _this.teacher = {};
                _this.modalTitle = "";
            }
        }
    }
</script>
